package com.company.cloud.page.modules.mgr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.page.modules.base.entity.PageConfigInfo;
import com.company.cloud.page.modules.base.service.PageConfigInfoService;
import com.company.cloud.page.modules.cache.models.PageInfoModel;
import com.company.cloud.page.modules.cache.utils.PageCachekeys;
import com.company.cloud.page.modules.cache.utils.PageConvertUtils;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigQuery4PageParam;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigSaveParam;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigUpdateParam;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigUpdateStateParam;
import com.company.cloud.page.modules.mgr.service.PageConfigMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.function.Function;

@Service
public class PageConfigMgrServiceImpl implements PageConfigMgrService {

    @Autowired
    private PageConfigInfoService pageConfigInfoService;


    @Override
    public IPage<PageInfoModel> query(PageConfigQuery4PageParam params) {
        IPage<PageConfigInfo> page = pageConfigInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<PageConfigInfo>()
                        .eq(StringUtils.hasText(params.getCompanyNo()),"company_no", params.getCompanyNo())
                        .like(StringUtils.hasText(params.getPageName()),"page_name", params.getPageName())
                        .eq(StringUtils.hasText(params.getPageType()),"page_type", params.getPageType())
                        .eq(StringUtils.hasText(params.getIsDraft()),"is_draft", params.getIsDraft())
                        .eq(StringUtils.hasText(params.getState()),"state", params.getState())
                        .eq(StringUtils.hasText(params.getIsTemplate()),"is_template",params.getIsTemplate())
                        .eq(StringUtils.hasText(params.getPagePositionNo()),"page_position_no", params.getPagePositionNo())
                        .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                        .orderByDesc("cdt")
        );
        IPage<PageInfoModel> data=page.convert(new Function<PageConfigInfo, PageInfoModel>() {
            @Override
            public PageInfoModel apply(PageConfigInfo pageConfigInfo) {
                return PageConvertUtils.getPageInfoModel(pageConfigInfo,params.isContent());
            }
        });
        return data;
    }

    @Override
    public PageConfigInfo save(PageConfigSaveParam params, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(params);
        if(StringUtils.hasText(params.getPagePositionNo())){
            //todo
            int positionNum= pageConfigInfoService.count(new QueryWrapper<PageConfigInfo>()
                    .eq("page_position_no",params.getPagePositionNo())
                    .eq("company_no",params.getCompanyNo())
            );
            if(positionNum>0){
                throw new BusinessException(BaseReturnEnum.PARAM_ERROR);
            }
        }
        PageConfigInfo entity = new PageConfigInfo();
        BeanUtil.copyProperties(params, entity);
        entity.setPageNo(PageCachekeys.getPageNo());
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        pageConfigInfoService.save(entity);
        return entity;
    }

    @Override
    public PageConfigInfo update(PageConfigUpdateParam params, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(params);
        PageConfigInfo entity = pageConfigInfoService.getById(params.getPageNo());
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BeanUtil.copyProperties(params, entity,"pagePositionNo");
        entity.addEditParam(userInfo);
        pageConfigInfoService.updateById(entity);
        return entity;
    }

    @Override
    public PageConfigInfo updateState(PageConfigUpdateStateParam params, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(params);
        PageConfigInfo entity = pageConfigInfoService.getById(params.getPageNo());
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        if(params.getState().equals(entity.getState())){
            return  null;
        }
        entity.setState(params.getState());
        entity.addEditParam(userInfo);
        pageConfigInfoService.updateById(entity);
        return entity;
    }

    @Override
    public PageConfigInfo delete(String pageNo, LoginSimpleUserInfo userInfo) {
        PageConfigInfo entity = pageConfigInfoService.getById(pageNo);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
        entity.addEditParam(userInfo);
        pageConfigInfoService.updateById(entity);
        return entity;
    }




}
